package io.renren.modules.noc.service.impl;

import io.renren.modules.noc.entity.*;
import io.renren.modules.noc.service.NocColsGroupService;
import io.renren.modules.noc.service.NocTemplateService;
import io.renren.modules.noc.service.NocWenjuanOptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.noc.dao.NocWenjuanDao;
import io.renren.modules.noc.service.NocWenjuanService;


@Service("nocWenjuanService")
public class NocWenjuanServiceImpl extends ServiceImpl<NocWenjuanDao, NocWenjuanEntity> implements NocWenjuanService {

    @Autowired
    private NocTemplateService nocTemplateService;
    @Autowired
    private NocColsGroupService nocColsGroupService;
    @Autowired
    private NocWenjuanOptionService nocWenjuanOptionService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NocWenjuanEntity> page = this.page(
                new Query<NocWenjuanEntity>().getPage(params),
                new QueryWrapper<NocWenjuanEntity>()
        );

        for(NocWenjuanEntity nocWenjuanEntity : page.getRecords()){
            NocTemplateEntity nocTemplateEntity = nocTemplateService.getById(nocWenjuanEntity.getTid());
            if(nocTemplateEntity != null){
                nocWenjuanEntity.setTname(nocTemplateEntity.getTemplate());
            }
        }

        for(NocWenjuanEntity nocWenjuanEntity : page.getRecords()){
            NocColsGroupEntity nocColsGroupEntity = nocColsGroupService.getById(nocWenjuanEntity.getColId());
            if(nocColsGroupEntity!=null){
                nocWenjuanEntity.setColName(nocColsGroupEntity.getColsName());
            }
        }

        return new PageUtils(page);
    }

    @Override
    public String getWenjuan(String tid){
        /**
        <li>
                <div class="top">
                    <i>A</i><span>您关注的同期活动</span>
                </div>
                <div class="bottom">
                    <label><input type="radio" name="zidingyi8" value=""> </label>
                    <label><input type="radio" name="zidingyi8" value="江苏国茂减速机股份有限公司产品推荐会 | 江苏国茂减速机股份有限公司 | 4月9日 下午3点  | 青岛国际会展中心 3号馆	"> 江苏国茂减速机股份有限公司产品推荐会 | 江苏国茂减速机股份有限公司 | 4月9日 下午3点  | 青岛国际会展中心 3号馆	</label>
                    <label><input type="radio" name="zidingyi8" value="不关注任何同期论坛"> 不关注任何同期论坛</label>
                </div>
            </li>
        */
        String result = "";

        Map<String,Object> p = new HashMap<>();
        p.put("tid",tid);
        List<NocWenjuanEntity> list = (List<NocWenjuanEntity>) this.listByMap(p);
        int code = 1;
        for(NocWenjuanEntity nocWenjuanEntity:list) {
            Integer type = nocWenjuanEntity.getType();
            if(type==null){
                type = 1;
            }

            result += "<li>";
            result += " <div class=\"top\">";
            result += "     <i>" + code + "</i><span>" + nocWenjuanEntity.getTitle() + "</span>";
            result += " </div>";

            Map<String, Object> params = new HashMap<>();
            params.put("wenjuan_id", nocWenjuanEntity.getId());
            List<NocWenjuanOptionEntity> options = (List<NocWenjuanOptionEntity>) nocWenjuanOptionService.listByMap(params);
            if (options != null && options.size() > 0) {
                result += " <div class=\"bottom\">";
            }
            for (NocWenjuanOptionEntity nocWenjuanOptionEntity : options) {
                if (type.intValue() == 1) {
                    NocColsGroupEntity nocColsGroupEntity = nocColsGroupService.getById(nocWenjuanEntity.getColId());
                    result += "     <label><input type=\"radio\" name=\"" + nocColsGroupEntity.getColsCode() + "\" value=\"" + nocWenjuanOptionEntity.getOptions() + "\"> " + nocWenjuanOptionEntity.getOptions() + " </label>";
                }
                if(type.intValue()==2){
                    NocColsGroupEntity nocColsGroupEntity = nocColsGroupService.getById(nocWenjuanEntity.getColId());
                    result += "     <label><input class=\"duoxuan\" type=\"checkbox\" name=\"" + nocColsGroupEntity.getColsCode() + "\" value=\"" + nocWenjuanOptionEntity.getOptions() + "\"> " + nocWenjuanOptionEntity.getOptions() + " </label>";
                }
            }
            if (options != null && options.size() > 0) {
                result += " </div>";
            }

            result += "</li>";

            code ++;
        }
        return result;
    }

}
